package cn.sqlexperiment.web.servlet;

import cn.sqlexperiment.web.classes.CaseData;
import cn.sqlexperiment.web.classes.PrescripData;
import cn.sqlexperiment.web.classes.RoundData;
import com.mysql.cj.jdbc.MysqlDataSource;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.*;

@WebServlet("/ReturnSectionServlet")
public class ReturnSectionServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String Sectname = req.getParameter("Sectname").split(" ")[0];
        System.out.println(Sectname);
        RoundData rd;
        try {
            rd=InSql(Sectname);
        } catch (SQLException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        //System.out.println(pd.getName()+" "+pd.getCaseno()+" "+cd.getPatino()+" "+cd.getTime()+" "+cd.getResult());
        req.setAttribute("RoundData1", rd);
        RequestDispatcher dis = req.getRequestDispatcher("showsection.jsp");
        dis.forward(req, resp);
        resp.setHeader("refresh", "1;url=showsection.jsp");
    }
    public static RoundData InSql(String Sectname) throws SQLException, ClassNotFoundException {
        /*DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/hospital?characterEncoding=utf8&useSSl=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("12345");
        Connection connection = dataSource.getConnection();*/
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://127.0.0.1:3306/hospital?useSSL=false&serverTimezone=UTC";
        String username = "root";
        String password = "12345";
        Connection connection = DriverManager.getConnection(url, username, password);

        RoundData rd = selectData(connection, Sectname);
        //System.out.println(cd.getName()+" "+cd.getCaseno()+" "+cd.getPatino()+" "+cd.getTime()+" "+cd.getResult());

        connection.close();

        return rd;
    }
    private static RoundData selectData(Connection connection,String Sectname) throws SQLException {
        String sql = "SELECT * FROM section WHERE sect_name = ?";
        //ArrayList<User> array=new ArrayList<>();
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1, Sectname);
        ResultSet resultSet = statement.executeQuery();
        RoundData rd=new RoundData();
        //String pati_name=resultSet.getString("pati_name");
        while(resultSet.next()) {
            String sect_no=resultSet.getString("sect_no");
            String sect_name=resultSet.getString("sect_name");
            String sect_tele=resultSet.getString("sect_tele");
            rd=new RoundData(sect_no,sect_name,sect_tele);
        }
        //System.out.println(Patino+" "+cd.getName()+" "+cd.getCaseno()+" "+cd.getPatino()+" "+cd.getTime()+" "+cd.getResult());
        resultSet.close();
        statement.close();
        return rd;
    }
}

